<?php
class ImagexController extends CBaseController
{
	
	protected $imagex;
	protected $validator;
	
	public function __construct($config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->reqLogin();
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
	}
	
	public function all()
	{
	 $imagexs = R::findAll('image',' ORDER BY name');
	 $this->templator->assign('imagexs',array_merge($imagexs, array()));
	 $this->templator->display('imagex_list.html');
	}
	
	public function savePrepare()
	{
		$this->imagex = R::dispense('image');
		$this->imagex->name = $_POST['name'];
		 if(isset($_FILES["image"]["name"]) && !empty($_FILES["image"]["name"]))
			 {
				if( $_FILES["image"]["error"] > 0)
				{
					$this->templator->assign('upload_errors','There is problem in file upload.Please try again');
				}
				else
				{
					$this->imagex->path = $_FILES["image"]["name"];
					move_uploaded_file($_FILES["image"]["tmp_name"],$this->getObject('CurrentRoute:Upload').'/'.$_FILES["image"]["name"]);
					
				}
			 }
	}
	
	public function updatePrepare()
	{
		$id = $_POST['id'];
		$this->imagex = R::load('image',$id);
		$this->imagex->name = $_POST['name'];
		 if(isset($_FILES["image"]["name"]) && !empty($_FILES["image"]["name"]))
			 {
				if( $_FILES["image"]["error"] > 0)
				{
					$this->templator->assign('upload_errors','There is problem in file upload.Please try again');
				}
				else
				{
					$this->imagex->path = $_FILES["image"]["name"];
					move_uploaded_file($_FILES["image"]["tmp_name"],$this->getObject('CurrentRoute:Upload').'/'.$_FILES["image"]["name"]);
				}
			 }
	}
	
	public function saveORupdateValidate()
	{
		$this->validator = new FormValidator();
		$this->validator->addValidation("name","req","Please fill in Name");
		return $this->validator->ValidateForm();
	}
		
	
	public function create()
	{
		$this->imagex = R::dispense('image');
		$this->templator->assignByRef('imagex',$this->imagex);
		$this->templator->display('imagex_general.html');
	}
	public function save()
	{
	  $this->savePrepare();
	  if($this->saveORupdateValidate())
	  {
		R::store($this->imagex);
		$this->templator->assign('info','Imagex added sucessfully');
		$this->templator->assignByRef('imagex',$this->imagex);		
		$this->templator->display('imagex_general.html');	
	  }
	  else
	  {
		$error_hash = $this->validator->GetErrors();
		$this->templator->assign('validate_errors',$error_hash);
		$this->templator->assignByRef('imagex',$this->imagex);
		$this->templator->display('imagex_general.html');
	  }
	}
	
	public function edit()
	{
		$id = $_GET['id'];
		if(isset($id))
		$this->imagex = R::load('image',$id);
		else
		$this->imagex = R::dispense('image');
		$this->templator->assignByRef('imagex',$this->imagex);		
		$this->templator->display('imagex_general.html');
	}
	
	public function update()
	{
		$this->updatePrepare();
		  if($this->saveORupdateValidate())
		  {
			R::store($this->imagex);
			$this->templator->assignByRef('imagex',$this->imagex);
			$this->templator->assign('info','Imagex updated sucessfully');
			$this->templator->display('imagex_general.html');	
		  }
		  else
		  {
			$error_hash = $this->validator->GetErrors();
			$this->templator->assign('validate_errors',$error_hash);
			$this->templator->assignByRef('imagex',$this->imagex);
			$this->templator->display('imagex_general.html');
		  }
	}
	
	public function delete()
	{
	 $id = $_GET['id'];
	 if(isset($id))
	 {
		$imagex = R::load('image',$id);
		if(isset($imagex))
		{
			R::trash($imagex);
			$this->templator->assign('info','Imagex deleted sucessfully');
		}
	 }
	 $this->all();
	}
}	